#! /usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Mosson"
from rest_framework import serializers

from .models import CourseCategory, Course, Teacher, CourseLesson, CourseChapter


class CourseCategoryModelSerializer(serializers.ModelSerializer):
    """
    课程分类序列化器
    """

    class Meta:
        model = CourseCategory
        fields = ["id", "name"]


class TeacherModelSerializer(serializers.ModelSerializer):
    """ 老师信息的序列化器"""

    class Meta:
        model = Teacher
        fields = ["id", "image", "name", "title", "signature", "brief"]


class CourseModelSerializer(serializers.ModelSerializer):
    """ 列表页课程的序列化器 """
    # teacher = TeacherModelSerializer(many=True) # 如果是多对一 就要many=True
    # 序列化器嵌套，返回外键对应的序列化器的数据, 嵌套的序列化器一定是一个外键
    teacher = TeacherModelSerializer()

    class Meta:
        model = Course
        fields = ["id", "name", "students", "lessons", "pub_lessons", "price", "course_img", "teacher",
                  "lessons_list", "discount_name", "real_price"]


class CourseLessonModelSerializer(serializers.ModelSerializer):
    """"""

    class Meta:
        model = CourseLesson
        fields = ["id", "lesson", "name", "free_trail"]


class CourseRetrieveModelSerializer(serializers.ModelSerializer):
    """ 课程详情序列化器 """
    teacher = TeacherModelSerializer()

    class Meta:
        model = Course
        fields = ["id", "name", "students", "lessons", "pub_lessons", "price", "course_img", "teacher",
                  "lessons_list", "level_name", "brief_html", "course_video", "discount_name", "real_price",
                  "activity_time"]


class CourseChapterModelSerializer(serializers.ModelSerializer):
    """
    详情页课程章节列表
    """
    # many=True 表示 序列化多个内容
    coursesections = CourseLessonModelSerializer(many=True)

    class Meta:
        model = CourseChapter
        fields = ["id", "chapter", "name", "coursesections"]
